const common = {
  namespaced: true,
  state: {
    init: {
      _ActivityTypeList: {},
      _BannerList: {},
      _CourseTypeList: {},
    },
    shop: {
      typeList: [],
      shopList: [],
      info: {
        _Goods: {},
        _GoodsRelease: {}
      }
    },
    tabsData: {
      type: null,
      deploy: {
        Page: 1,
        Rows: 5
      },
      list: []
    },
    course: {
      list: {
        _CourseList: {},
        _CourseType: {},
      },
      info: {
        _Course: {},
        _CourseRelease: {}
      },
    }
  },
  mutations: {
    getActId(state, payload) {
      let id = "";
      for (let i = 0; i < state.init._ActivityTypeList.length; i++) {
        if (payload.name == state.init._ActivityTypeList[i].ActivityType_Name) {
          id = state.init._ActivityTypeList[i].ActivityType_ID;
          break;
        }
      }
      return id
    },
    tabsDataCourse(state, payload) {
      state.tabsData.list = state.init._CourseTypeList;
      state.tabsData.type = null;
    },
    //微信支付
    wxPay(state, payload) {
      function onBridgeReady() {
        WeixinJSBridge.invoke(
          'getBrandWCPayRequest', {
            "appId": payload.appId, //公众号名称，由商户传入     
            "timeStamp": payload.timeStamp, //时间戳，自1970年以来的秒数     
            "nonceStr": payload.nonceStr, //随机串     
            "package": payload.package,
            "signType": "MD5", //微信签名方式：     
            "paySign": payload.paySign //微信签名 
          },
          function (res) {
            if (res.err_msg == "get_brand_wcpay_request:ok") {
              // 使用以上方式判断前端返回,微信团队郑重提示：
              //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
              global.router.replace({
                name: "myCourses",
                query: {
                  state: 'wxPay'
                },
              });
              Toast.success('支付成功！');
            }
          }
        );
      }
      if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
          document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
          document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
      } else {
        onBridgeReady();
      }
    },
    //微信支付
    _wxPay(state, payload) {
      function onBridgeReady() {
        WeixinJSBridge.invoke(
          'getBrandWCPayRequest', {
            "appId": payload.appId, //公众号名称，由商户传入     
            "timeStamp": payload.timeStamp, //时间戳，自1970年以来的秒数     
            "nonceStr": payload.nonceStr, //随机串     
            "package": payload.package,
            "signType": "MD5", //微信签名方式：     
            "paySign": payload.paySign //微信签名 
          },
          function (res) {
            if (res.err_msg == "get_brand_wcpay_request:ok") {
              // 使用以上方式判断前端返回,微信团队郑重提示：
              //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
              global.router.replace({
                name: "myShop",
                query: {
                  state: 'wxPay'
                },
              });
              Toast.success('支付成功！');
            }
          }
        );
      }
      if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
          document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
          document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
      } else {
        onBridgeReady();
      }
    },
  },
  actions: {
    GetCourseList(context, param) {
      // 课程列表
      global.POST(global.api.GetCourseList, {
        CourseTypeID: param
      }).then(res => {
        context.state.course.list = res
      })
    },
    GetCourseInfo(context, param) {
      // 课程列表
      global.POST(global.api.GetCourseInfo, {
        CourseReleaseID: param
      }).then(res => {
        context.state.course.info = res
      })
    },
    getInit(context, param) {
      // 首页初始化
      global.POST(global.api.Init, {
        Phone: param
      }).then(res => {
        context.state.init._ActivityTypeList = res._ActivityTypeList
        context.state.init._BannerList = res._BannerList
        context.state.init._CourseTypeList = res._CourseTypeList
        context.state.tabsData.list = res._CourseTypeList
        if (param != undefined) {
          param.suc()
        }
      })
    },
    GetTypeList(context, param) {
      // 获取商品类型
      global.POST(global.api.GetTypeList, {}).then(res => {
        context.state.shop.typeList = res._GoodsTypeList
        param.suc(res._GoodsTypeList[0].GoodsType_ID)
      })
    },
    GetGoodsList(context, param) {
      // 获取商品列表
      global.POST(global.api.GetGoodsList, {
        GoodsTypeID: param.id,
        Page: param.deploy.Page,
        Rows: param.deploy.Rows,
      }).then(res => {
        if (param.deploy.Page == 1)
          context.state.shop.shopList = res._CourseList
        else
          context.state.shop.shopList = context.state.shop.shopList.concat(res._CourseList)
        if (param.ref != undefined) {
          if (res._CourseList.length < param.deploy.Rows) {
            param.ref.finishInfinite(true)
          } else {
            param.ref.finishPullToRefresh()
            if (param.pageAdd != undefined)
              param.pageAdd()
          }
        }
        if (param.success != undefined) {
          return param.success()
        }
      })
    },
    GetGoodsInfo(context, param) {
      // 获取商品详情
      global.POST(global.api.GetGoodsInfo, {
        GoodsReleaseID: param
      }).then(res => {
        context.state.shop.info = res
      })
    },
    GetActList(context, param) {
      // 获取活动列表
      if (param.type != undefined) {
        context.state.tabsData.type = param.type
      }
      if (param.Page != undefined) {
        context.state.tabsData.deploy.Page = 1;
      }
      global.POST(global.api.GetActList, {
        ActivityTypeID: context.state.tabsData.type,
        Page: context.state.tabsData.deploy.Page,
        Rows: context.state.tabsData.deploy.Rows
      }).then(res => {
        if (context.state.tabsData.deploy.Page == 1)
          context.state.tabsData.list = res._ActivityList
        else
          context.state.tabsData.list = context.state.tabsData.list.concat(res._ActivityList)
        if (param.ref != undefined) {
          if (res._ActivityList.length < context.state.tabsData.deploy.Rows) {
            param.ref.finishInfinite(true)
          } else {
            param.ref.finishPullToRefresh()
            context.state.tabsData.deploy.Page++
          }
        }
        if (param.suc != undefined) {
          param.suc()
        }
      })
    },
    CourseCommit({
      commit
    }, param) {
      // 课程报名
      global.POST(global.api.CourseCommit, {
        CourseReleaseID: param
      }).then(res => {
        if(global.$store.state.home.course.info._Course.Course_Type == 1){
          global.router.replace({
            name: "myCourses",
            query: {
              state: 'wxPay'
            },
          });
          Toast.success('购买成功！');
          return
        }
        commit('wxPay', res)
      })
    },
    GoodsBuy({
      commit
    }, param) {
      // 商品支付
      global.POST(global.api.GoodsBuy, {
        Consumption_GoodsReleaseID: param.id,
        Consumption_Count: param.count,
      }).then(res => {
        commit('_wxPay', res)
      })
    }
  },
  getters: {}
}
export default common